package com.foreveredu.propms.web.struts.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.ChargeCriterion;
import com.foreveredu.propms.domain.ChargeItem;
import com.foreveredu.propms.domain.House;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.service.IChargeService;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.SingleChargeCriterionActionForm;

public class SingleChargeCriterionAction extends AbstractBaseDispatchAction {

	private IChargeService chargeService = null;
	private IManagementService managementService = null;
	
	public IChargeService getChargeService() {
		return chargeService;
	}
	
	public void setChargeService(IChargeService chargeService) {
		this.chargeService = chargeService;
	}

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}
	
	// 跳转到单独物业收费标准列表页面
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if (offset < 0) {
			return mapping.findForward("singleCriterionList");
		}
		//获得单独物业收费标准列表(翻页)
		PageModel pm = chargeService.findSingChargeCriterionsList(offset);
		request.setAttribute("pm", pm);

		return mapping.findForward("singleCriterionList");
	}

	// 跳转到添加单独收费标准页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

//		//获得收费项目列表，目前暂时单独收费标准只是针对物业服务费
//		List<ChargeItem> chargeItemList = new ArrayList<ChargeItem>();
//		ChargeItem item = chargeService.findChargeItemByItemId("2");
//		chargeItemList.add(item);
		
		//2013/10/4修改需求，所有收费项目均可设置单独收费标准
		List<ChargeItem> chargeItemList = chargeService.findChargeItemsByType(1);
		
		request.setAttribute("chargeItemList", chargeItemList);
		
		return mapping.findForward("toAdd");
	}
	
	// 完成单独物业收费标准添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//获得表单数据
		SingleChargeCriterionActionForm sccaf = (SingleChargeCriterionActionForm)form;

//		//获得收费项目列表，目前暂时单独收费标准只是针对物业服务费
//		List<ChargeItem> chargeItemList = new ArrayList<ChargeItem>();
//		ChargeItem item = chargeService.findChargeItemByItemId("2");
//		chargeItemList.add(item);

		//2013/10/4修改需求，所有收费项目均可设置单独收费标准
		List<ChargeItem> chargeItemList = chargeService.findChargeItemsByType(1);
		
		request.setAttribute("chargeItemList", chargeItemList);
		
		//表单验证
		if(sccaf.getChargeItemId() == 0) {
			request.setAttribute("chargeItemError", "请选择收费项目！");
			return mapping.findForward("toAddError");
		}
		if(sccaf.getHouseId() == null || "".equals(sccaf.getHouseId())) {
			request.setAttribute("houseIdError", "请输入9位房产编号！");
			return mapping.findForward("toAddError");
		} else if (managementService.findHouseByHouseId(sccaf.getHouseId()) == null) {
			request.setAttribute("houseNotFoundError", "房产编号输入有误，找不到此房产！");
			return mapping.findForward("toAddError");
		}
		if(sccaf.getAmount() == 0) {
			request.setAttribute("amountError", "请输入收费金额！");
			return mapping.findForward("toAddError");
		}
		
		ChargeCriterion cc = new ChargeCriterion();
		cc.setChargeItem(chargeService.findChargeItemById(sccaf.getChargeItemId()));
		
		//设置单独收费标准所对应的房产及所在小区
		House house = managementService.findHouseByHouseId(sccaf.getHouseId());
		cc.setHouse(house);
		cc.setSubDistrict(house.getBuilding().getSubDistrict());
		
		cc.setAmount(sccaf.getAmount());
		cc.setType(1);//设置"单独收费标准"状态
		cc.setEnabled(1);//设置"有效"状态
		
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		cc.setCreateDate(date);//设置创建日期	
		
		//保存单独物业收费标准对象
		chargeService.saveChargeCriterion(cc);
		
		return mapping.findForward("toCriterionList");
	}

	// 跳转到收费标准修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		SingleChargeCriterionActionForm sccaf = (SingleChargeCriterionActionForm)form;
		ChargeCriterion chargeCriterion =chargeService.findChargeCriterionById(sccaf.getId());

		request.setAttribute("chargeCriterion", chargeCriterion);
		
		List<ChargeItem> chargeItemList = chargeService.findChargeItems();
		request.setAttribute("chargeItemList", chargeItemList);
		
		return mapping.findForward("toUpdate");
	}

	// 保存收费标准修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得表单数据
		SingleChargeCriterionActionForm sccaf = (SingleChargeCriterionActionForm)form;
		
		//表单验证
		if(sccaf.getChargeItemId() == 0) {
			request.setAttribute("chargeItemError", "请选择收费项目！");
			return mapping.findForward("toUpdateError");
		}
		if(sccaf.getHouseId() == null || "".equals(sccaf.getHouseId())) {
			request.setAttribute("houseIdError", "请输入9位房产编号！");
			return mapping.findForward("toUpdateError");
		} else if (managementService.findHouseByHouseId(sccaf.getHouseId()) == null) {
			request.setAttribute("houseNotFoundError", "房产编号输入有误，找不到此房产！");
			return mapping.findForward("toUpdateError");
		}
		if(sccaf.getAmount() == 0) {
			request.setAttribute("amountError", "请输入收费金额！");
			return mapping.findForward("toUpdateError");
		}
		
		ChargeCriterion chargeCriterion =chargeService.findChargeCriterionById(sccaf.getId());
		PropertyUtils.copyProperties(chargeCriterion, sccaf);
		
		chargeCriterion.setChargeItem(chargeService.findChargeItemById(sccaf.getChargeItemId()));
		chargeCriterion.setHouse(managementService.findHouseByHouseId(sccaf.getHouseId()));
		chargeCriterion.setAmount(sccaf.getAmount());
		chargeCriterion.setEnabled(1);
		
		chargeService.updateChargeCriterion(chargeCriterion);
		return mapping.findForward("updateSave");
	}

	// 完成收费标准删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		SingleChargeCriterionActionForm sccaf = (SingleChargeCriterionActionForm)form;
		chargeService.deleteChargeCriterionById(sccaf.getId());
		
		return mapping.findForward("toList");
	}

}
